from flask import Flask,render_template, request, jsonify
import sqlite3

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/save-foods', methods=['POST'])
def save_foods():
    data = request.get_json()
    foods = data.get('foods', [])

    # 连接数据库
    conn = sqlite3.connect('food_database.db')
    cursor = conn.cursor()

    # 插入食物名称到数据库
    for food in foods:
        cursor.execute('INSERT INTO foods (name) VALUES (?)', (food,))

    conn.commit()
    conn.close()

    return jsonify({'message': '食物数据已保存'})


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=50000, debug=True)
